/**
 * 创客贴引入代码
 */
define( [
  'jquery',
  '../../modules/createEditor',
  'message',
  '../../lib/v2'
], function( $, editorObj, msg ) {
  var returnParam = {};


  var isInit = false;
  var isCover = false;

  function columnsRange() {
    isInit = true;
    $( '.content-list>.design_div' ).show();
    $( '[event-columns="design"]' ).bwColumns( [ '社交媒体', '生活' ], undefined, '.design_wraper' );
  }

  returnParam.designInit = function() {
    if ( !isInit ) {
      columnsRange()
    }
  }

  $( '.design_div' ).on( 'click', '.design_item', function() {
    isCover = $( this ).hasClass( 'cover' );
  } );
  // 设置使用密钥
  $( '.design_div .chuangkit-design-button' ).attr( 'data-access', '2e5e299d43e44c6494f7624dd71f28aa' );
  // 创客贴的回调
  window.chuangkitComplate = function( eve, index ) {
    var imgsrc = location.protocol + eve.data;
    msg.show( '正在上传图片' );
    $.ajax( {
      type: 'post',
      url: editorObj.getActionUrl( 'catchimage' ),
      data: {
        source: [ imgsrc ]
      },
      success: function( data ) {
        msg.hide();
        data = typeof data === 'string' ? $.parseJSON( data ) : data;
        var imgObj = {};
        if ( data.list && data.list[ 0 ] && data.list[ 0 ].state === 'SUCCESS' ) {
          imgObj = data.list[ 0 ];
          if ( isCover ) {
            $( '.cover-input' ).val( imgObj.url ).trigger( 'change' );
          } else {
            editorObj.execCommand( 'inserthtml', '<img src="' + imgObj.url + '"/><br/>' );
          }
        } else {
          msg.showWarning( '上传失败，请重试', msg.showTime );
        }
      }
    } );
  }

  return returnParam;
} );
